#include<iostream>
#include<cstdio>
using namespace std;
const int N = 1e8 + 10;
int arr[N], q[N];
int n, k;
int main()
{
	scanf("%d%d", &n, &k);
	int hh = 0, tt = -1;
	for (int i = 0; i < n; i++) {
		scanf("%d", &arr[i]);
	}
	for (int i = 0; i < n; i++) {
		if (hh <= tt && i - k + 1 > q[hh]) {
			hh++;
		}
		while (hh <= tt && arr[q[tt]] >= arr[i]) {
			tt--;
		}
		q[++tt] = i;
		if (i >= k - 1) {
			cout << arr[q[hh]] << " ";
		}
	}
	cout << endl;
	hh = 0, tt = -1;
	for (int i = 0; i < n; i++) {
		if (hh <= tt && i - k + 1 > q[hh]) {
			hh++;
		}
		while (hh <= tt && arr[q[tt]] <= arr[i]) {
			tt--;
		}
		q[++tt] = i;
		if (i >= k - 1) {
			cout << arr[q[hh]] << " ";
		}
	}
	return 0;
}